<?php
/*
Template Name: Days Out
Auther: Naseer Ahnad
Created: 10-03-2012
*/
?>
<?php 
	///////////// get geta from dfatabase
	///////////////////////// Pagination //////////////////////////////
	$totalRowsPerPage=50;
	function makePaginationUrl($pageNo){
		$pVar='pgNo';
		$q=explode("&",$_SERVER['QUERY_STRING']);
		$done=false;
		for($i=0;$i<count($q);$i++){
			if(substr($q[$i],0,strlen($pVar)+1)==$pVar."="){
				$q[$i]=$pVar."=".$pageNo;
				$done=true;
				break;
			}
		}
		if($done!=true) $q[]=$pVar."=".$pageNo;
		$q=implode("&",$q);
		return "?".$q;
	}
	
	function makePaginationNext($totalRec, $totalRowsPerPage){
		if(isset($_GET['pgNo'])) $curPage=$_GET['pgNo']; else $curPage=1;
		$tp= ceil($totalRec/$totalRowsPerPage);
		if($curPage!=$tp) return makePaginationUrl($curPage+1); else return "";
	}

	function makePaginationPrevious(){
		if(isset($_GET['pgNo'])) $curPage=$_GET['pgNo']; else $curPage=1;
		if($curPage!=1) return makePaginationUrl($curPage-1); else return "";
	}
	
	function makePaginationCombo($totalRec, $totalRowsPerPage){
		$curPage=$_GET['pgNo'];
		$tp= ceil($totalRec/$totalRowsPerPage);
		$combo="<select onchange='javascript: window.location=this.value;'>";
		for($i=1;$i<=$tp;$i++){
			if($curPage==$i) $sel="selected='selected'"; else $sel="";
			$combo.="<option value='".makePaginationUrl($i)."' $sel>Page $i</option>\n";
		}
		$combo.="</select>";
		return $combo;
	}
	
	function makeLimit($totalRowsPerPage){
		if(isset($_GET['pgNo'])) $pageNo=$_GET['pgNo']; else $pageNo=1;
		$offset=($pageNo-1)*$totalRowsPerPage;
		return " limit ".$offset.",".$totalRowsPerPage;
	}
	
	global $wpdb;
	if(isset($_GET['county'])) $criteria="nearest_town = '".$_GET['county']."'"; else $criteria="1=1";
	$data = $wpdb -> get_results('select * from daysout Where '.$criteria.makeLimit($totalRowsPerPage), ARRAY_A);
	$tRec = $wpdb -> get_results('select COUNT(*) as c from daysout Where '.$criteria,  ARRAY_A);
	$tRec = intval($tRec[0]["c"]);
	
	$counties = $wpdb -> get_results('select DISTINCT(nearest_town)  from daysout where nearest_town<>"" order by nearest_town', ARRAY_A);
	
	//print_r($counties);
	
	get_header(); 
?>

    <div class="main-content-inner global-width">
    	
    	<section class="contents full-width">
           	<h1>Days Out</h1>
			
            <div>
            	<form name="filter" action="http://armchairweddingguru.co.uk/days-out-2/" method="get">
            	<table>
                	<tr>
                        <td><strong>Filter</strong> </td>
                        <td>
                            <select name="county" id="county">
                             <?php 
							 	foreach($counties as $county):
								if($_GET['county']==$county['nearest_town']) $sel='selected="selected"'; else $sel='';
							  ?>
                             <option value="<?php echo $county['nearest_town']; ?>" <?php echo $sel; ?> ><?php echo $county['nearest_town']; ?></option>
                             <?php endforeach; ?>   
                            </select>
                        </td>
                        <td><input type="submit" value="Filter" class="button float-left" /></td>
                    </tr>
                </table>
                </form>
            </div><br /><br />
			
            <table width="100%" border="0" class="nm_tables">
            <tr>
                <th width="283" align="center">Place Of Interest</th>
                <th width="168" align="center">Nearest Town</th>
                <th width="45" align="center">£0 - 5</th>
                <th width="45" align="center">£5 - 10</th>
                <th width="45" align="center">£10 - 15</th>
                <th width="45" align="center">£15 - 20 </th>
                <th width="45" align="center">£20 </th>
                <th width="84" align="center">Website</th>
            </tr>
        
            <?php foreach($data as $dataItem): ?>
            <tr>
                <td align="left"><?=$dataItem['place_of_interest']?></td>
                <td align="left"><?=$dataItem['nearest_town']?></td>
                <td width="45" align="center"><?=$dataItem['range_1']?></td>
                <td width="45" align="center"><?=$dataItem['range_2']?></td>
                <td width="45" align="center"><?=$dataItem['range_3']?></td>
                <td width="45" align="center"><?=$dataItem['range_4']?></td>
                <td width="45" align="center"><?=$dataItem['range_5']?></td>
                <td align="center">
                <?php if($dataItem['link']!="#VALUE!" or $dataItem['link']!=""): ?>
                    <a href="<?=$dataItem['link']?>">Visit site</a>
                 <?php endif; ?>
                </td>
            </tr>
            <?php endforeach; ?>
            </table>

<br />
<br />
            
            <div id="paginationBar" style="width:100%; margin:0 auto; text-align:right; margin-top:5px;">
            <?php
            $prevPage=makePaginationPrevious();
            $nextPage=makePaginationNext($tRec, $totalRowsPerPage);
            ?>
            <?php if($prevPage!=""): ?>
            <a href="<?=$prevPage?>">&lt;&lt;Prev</a>&nbsp;
            <?php endif; ?>
            <?=makePaginationCombo($tRec, $totalRowsPerPage);?>&nbsp;
            <?php if($nextPage!=""): ?>
            <a href="<?=$nextPage?>">Next&gt;&gt;</a>&nbsp;
            <?php endif; ?>
            </div>    
            


           	<div class="clear"></div>
            
    	</section>   
    	<div class="clearfix"></div>
    </div>

<!-- Main Content Ends -->
</section>

<?php get_footer(); ?>